[% setvar title Common attribute system to allow user-defined, extensible attributes %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Common attribute system to allow user-defined, extensible attributes</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 28 Sep 2000
  Last Modified: 1 Oct 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 337
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Camel-3 and others have proposed a syntax for declaring variables like
so:</p>
<pre>   my type $var :attr1 :attr2 = $val;</pre>
<p>However, nobody has firmly nailed down what <code>:attr1</code> and <code>:attr2</code> are
supposed to do. This takes a shot at it, since this could simplify the
implementation of <b>RFC 188</b>, <b>RFC 336</b>, <b>RFC 163</b>, and others.</p>
<p>Currently, the <code>attributes</code> module can be used to set and retrieve
these attributes. However, the current <code>MODIFY_ATTRIBUTES</code> and
<code>FETCH_ATTRIBUTES</code> subs are not really fine-grained enough to give
really good control. It would be nice to be able to call specialized,
delegateable and inheritable subs to handle attributes. Furthermore,
these attribute handlers should be able to alter Perl's internals
somewhat, perhaps through the <code>use optimize</code> pragma.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Attributes should be able to access some type of core hooks so that they
can be defined by the user and extended arbitrarily. For example, in
pseudocode, a user should be able to say something like this:</p>
<pre>   package Dog;

   attr fluffy {
        causes numeric contexts to fail;
        results in stringification appending &quot;--and fluffy&quot;;
   }</pre>
<p>Then, if a user does the following:</p>
<pre>   my Dog $spot :fluffy = &quot;happy&quot;;
   print &quot;$spot&quot;;                   # &quot;happy--and fluffy&quot;;
   $spot++;                         # fails </pre>
<p>So, the declaration of <code>:fluffy</code> would trigger the execution of a
specific attribute handler, which could make the necessary changes to
the variable.</p>
<p>This would allow <b>RFC 188</b>, which proposes new <code>private</code> and <code>public</code>
keywords, to instead be implemented as attributes. This is perhaps more
appropriate since these do not alter lexical scope (unlike <code>my</code> and
<code>our</code>), but rather change properties of the variables themselves:</p>
<pre>   package __ALL__;     # some type of builtin global declaration
    
   attr private {
        attachable to hashes and hash keys only;
        marks entire hash as non-autovivifying;
        marks specific entry as private to the package;
        allows duplication of keys in different packages;
        leaves any other entries public;
   }

   attr public {
        attachable to hashes and hash keys only;
        marks specific entry as accessible by all packages;
        would take the entry out of the package symbol table;
   }</pre>
<p>In your code, then, you could use the <code>:private</code> and <code>:public</code>
attributes to modify your variables:</p>
<pre>   sub new {
         my ($class, %self) = @_;
         bless \%self :private, $class;
         $self{seed} = rand;             # dies, can't autovivify
         $self{seed} :private = rand;    # okay
         $self{seed} = rand;             # now okay
   }</pre>
<p>In addition, perhaps we have a BigInt class that we need to be able to
modify:</p>
<pre>   package BigInt;

   attr 128bit {
        attachable to any variable;
        causes exception if 128 bits not supported;
        results in huge memory preallocation;
        does other neato stuff too; 
   }</pre>
<p>Again, in your code:</p>
<pre>   my BigInt $x :128bit;</pre>
<p>This would invoke the attribute to modify the variable's properties
internally. By having some type of attribute-specific declaration
method, the attribute system could be modifiable at will, allowing for
native access to variable manipulation without the need to compile a new
version of Perl.</p>
<p>This would allow those who want to to warp Perl OO into Java or Python
or C++ without these features having to be either widely used or
embedded in core. A base class could simply define attribute handlers
which other classes could then inherit from. Attributes would be
inherited just like subs.</p>
<p>Attributes are not necessarily tied to <code>my</code> or <code>our</code> declarations; see
<b>RFC 279</b> for details.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>The easiest way I see is to change the <code>attributes</code> pragma into a
pre-declaration pragma instead, something like:</p>
<pre>   package Foo;
   use attributes fluffy =&gt; 'DEFAULT',
                  size =&gt; \&amp;SIZE_ATTR,
                  UNKNOWN =&gt; \&amp;ATTR_HANDLER;</pre>
<p>So, the declaration of <code>:fluffy</code> on an instance of <code>Foo</code> would just
result in it being stored as text retrievable via <code>attributes::get</code> or
some other means. On declaration of <code>:size('big')</code>, though, the
<code>SIZE_ATTR(\$var, 'big')</code> sub from the class would be called. That is,
a reference to the variable being altered would be the first arg, and
the attribute arguments would be passed in by value.</p>
<p>Any unknown attributes would be thrown to the <code>ATTR_HANDLER</code> sub, which
would take the name of the attribute as the first arg, and then the
other args would look like any other handler. For example:</p>
<pre>   my Foo $bar :baz('likely');  # Foo-&gt;ATTR_HANDLER('baz', \$bar,
                                #                   'likely')</pre>
<p>The name of the unknown attribute is the first arg so it can easily be
shifted off.</p>
<p>In both scenarios, the attributes should always be retrievable at a
later time. However, the declaration of an attribute should also be able
to trigger the execution of a specific sub that can make changes to
internals (perhaps through the <code>use optimize</code> pragma), to the data for
that object, or something else.</p>
<p>Note that you could even add a <code>use strict 'attrs'</code> pragma that
dictated you could only use predeclared attributes. This could help
catch mistyping of important ones:</p>
<pre>   package Parallel;
   use attributes slippery =&gt; \&amp;FALL_OFF;

   package main;
   use strict 'attrs';
   my Parallel $bar :slipery;    # raise exception</pre>
<p>Here, we've caught a typo that might result in badness later.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>This alters the current meaning of attribute handling. From the man page
on <code>attributes.pm</code>:</p>
<pre>   WARNING: the mechanisms described here are still experimental.
   Do not rely on the current implementation.  In particular, there
   is no provision for applying package attributes to 'cloned' copies
   of subroutines used as closures.  (See the Making References entry
   in the perlref manpage for information on closures.)  Package-
   specific attribute handling may change incompatibly in a future
   release.</pre>
<p>Like Michael Schwern said in RFC 241, &quot;You pays your money and you takes
your chances.&quot; ;-)</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 279: my() syntax extensions and attribute declarations</p>
<p>RFC 218: <code>my Dog $spot</code> is just an assertion</p>
<p>RFC 303: Keep <code>use less</code>, but make it work.</p>
<p>RFC 270: Replace XS with the <code>Inline</code> module as the standard way to extend Perl.</p>
<p>RFC 188: Objects : Private keys and methods</p>
<p>RFC 163: Objects: Autoaccessors for object data structures</p>
<p>RFC 336: use strict 'objects': a new pragma for using Java-like objects in Perl</p>
</div>
